<?php
//
// +------------------------------------------------------------------------+
// | PHP Version 5.4                                                        |
// +------------------------------------------------------------------------+
// | Copyright (c) All rights reserved.                                     |
// +------------------------------------------------------------------------+
// | 世纪车来车往(北京）网络科技有限公司 版权所有！                                 |
// | TEL:010-63732365  HOTLINE:400-6688-365  EMAIL:clcw@clcw.com.cn         |
// | $Revision: 1.0 $                                                       |
// +------------------------------------------------------------------------+
// | Author: xiangminghu@clcw.com.cn                                        |
// +------------------------------------------------------------------------+
//

namespace AuctionBack\Logic;


class UserCashLogic extends BaseLogic
{
    protected $autoCheckFields = false;

    protected $userCashModel;

    public function _initialize()
    {
        $this->userCashModel = D('UserCash', 'Model');
    }

    public function getCashList($page, $pageSize, $params)
    {

        $where = [];
        if (isset($params['status'])) {
            $where['c.status'] = $params['status'];
        }

        if ($params['userId']) {
            $where['c.user_id'] = $params['userId'];
        }
        if ($params['mobile']) {
            $where['u.mobile'] = $params['mobile'];
        }
        if ($params['realName']) {
            $where['u.real_name'] = $params['realName'];
        }
        if ($params['icard']) {
            $where['u.icard'] = $params['icard'];
        }
        if ($params['startTime'] && $params['endTime']) {
            $where['c.create_time'] = ['between', [$params['startTime'], $params['endTime']]];
        } elseif ($params['endTime']) {
            $where['c.create_time'] = ['lt', $params['endTime']];
        } elseif ($params['startTime']) {
            $where['c.create_time'] = ['gt', $params['startTime']];
        }
        $total = $this->userCashModel->alias('c')
            ->join('left join clcw_user u ON c.user_id=u.user_id')
            ->where($where)->count();

        $amount = $this->userCashModel->alias('c')->field('SUM(c.amount) amount')
            ->join('left join clcw_user u ON c.user_id=u.user_id')
            ->where($where)->find();

        $cols = 'c.id,c.user_id,u.mobile,u.real_name,u.icard,c.amount,c.status,
                c.card,c.bank,c.status,c.remark,c.create_time,c.withdraw_time';

        $list = $this->userCashModel->alias('c')->field($cols)
            ->join('left join clcw_user u ON c.user_id=u.user_id')
            ->where($where)
            ->page($page, $pageSize)
            ->select();

        return [
            'list'   => $list,
            'total'  => $total,
            'amount' => $amount['amount']
        ];
    }

    /**
     * @param int $id
     *
     * @return bool
     */
    public function getUserCashInfo($id = 0)
    {
        if (empty($id)) {
            return false;
        }
        return $this->userCashModel->where(['id' => $id])->find();
    }
}